package edu.upc.clase.demo.controller;

import edu.upc.clase.demo.entity.Credential;
import edu.upc.clase.demo.entity.CriterioBusqueda;
import edu.upc.clase.demo.entity.JsonResponse;
import edu.upc.clase.demo.entity.ServicioAdicional;
import edu.upc.clase.demo.service.ServicioAdicionalService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller("ServicioAdicionalController")
public class ServicioAdicionalController {
    
    @Autowired
    private ServicioAdicionalService servicioAdicionalService;
    private static Logger log = LoggerFactory.getLogger(ServicioAdicionalController.class);
    
    @RequestMapping("/servicioAdicional/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("servicioAdicional/index");
        List<ServicioAdicional> servicioAdicional = servicioAdicionalService.buscarTodos();
        CriterioBusqueda criterioBusqueda = new CriterioBusqueda();
        mav.addObject("servicioAdicional", servicioAdicional);
        mav.addObject("criterioBusqueda",criterioBusqueda);
        return mav;
    }
 
    @RequestMapping(value = "/servicioAdicional/new", method = RequestMethod.GET)
    public ModelAndView newServicioAdicional() {
        ModelAndView mav = new ModelAndView("servicioAdicional/new");
        ServicioAdicional servicioAdicional = new ServicioAdicional();
        mav.getModelMap().put("servicioAdicional", servicioAdicional);
        return mav;
    }
    
     @RequestMapping(value = "/servicioAdicional/new", method = RequestMethod.POST)
    public String createServicioAdicional(@ModelAttribute("servicioAdicional")ServicioAdicional servicioAdicional, 
                                SessionStatus status,
                                HttpServletRequest request) {
    
   servicioAdicionalService.insertar(servicioAdicional);
   
   /*
   try {
            MultipartFile avatar = servicioAdicional.getAvatar();

            if (avatar.getSize() > 0) {
                File dir = new File(request.getSession().getServletContext().getRealPath("") 
                                       + "/upload/");
                
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                File destinationFile = new File(request.getSession().getServletContext().getRealPath("") 
                                       + "/upload/"+ avatar.getOriginalFilename());
                avatar.transferTo(destinationFile);
                             
            }
    } catch (IOException iOException) {
            log.error("No se pudo guardar la imagen");
        }
   */     
        status.setComplete();

        return "redirect:/pages/servicioAdicional/index";
        
    }
   
  @RequestMapping(value = "/servicioAdicional/edit", method = RequestMethod.GET)
    public ModelAndView editServicioAdicional(@RequestParam("descSerAdic")String descSerAdic) {    
        ModelAndView mav = new ModelAndView("servicioAdicional/edit");
        ServicioAdicional servicioAdicional = servicioAdicionalService.buscar(descSerAdic);
        mav.getModelMap().put("servicioAdicional", servicioAdicional);
        return mav;        
    }
    
    @RequestMapping(value="/servicioAdicional/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("servicioAdicional") ServicioAdicional servicioAdicional, SessionStatus status) {
        servicioAdicionalService.actualizar(servicioAdicional);
        status.setComplete();
        return "redirect:/pages/servicioAdicional/index";
    }
    
    @RequestMapping("/servicioAdicional/delete")
    public ModelAndView delete(@RequestParam("id")String descSerAdic)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/servicioAdicional/index");
        ServicioAdicional servicioAdicional = servicioAdicionalService.buscar(descSerAdic);
        servicioAdicionalService.eliminar(servicioAdicional);
        return mav;
    }
    
    /*@RequestMapping("/servicioAdicional/login")
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView("servicioAdicional/login");
        Credential credential = new Credential();
        mav.getModelMap().put("credential", credential);
        return mav;
    } 
    */   
        
    @RequestMapping("/servicioAdicional/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/pages/servicioAdicional/login";
    }        
       
    @RequestMapping(value = "/listarServicioAdicional", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody JsonResponse listMyFiles() {
        List<ServicioAdicional> servicioAdicional = servicioAdicionalService.buscarTodos();
        return new JsonResponse(true, servicioAdicional);
    }
  }